<template>
  <div style="height: 50%; line-height: 50%;">
    <body id="login-page" align="center" >
    <el-form ref="loginForm" :model="loginForm" :rules="rules" class="login-container" label-position="left" label-width="0px">
      <h3 class="login_title">系统登录</h3>
      <el-form-item prop="account">
        <el-input

            v-model="loginForm.account"
            auto-complete="off"
            placeholder="账号"
            type="text"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
            v-model="loginForm.password"
            auto-complete="off"
            placeholder="密码"
            show-password></el-input>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
            style="width: 40%;  border: none"
            type="primary"
            @click="login('loginForm')"
        >登录</el-button>
        <el-button
            style="width: 40%;  border: none"
            type="primary"
            @click="regester"
        >注册</el-button>
      </el-form-item>
    </el-form>
    </body>
  </div>

</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      data:null,
      token:null,
      user:null,
      factoryid:null,
      factorystatusid:null,

      loginForm: {
        account: "",
        password: "",
      },
      responseResult: [],
      rules:{
        account: [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ],

      },
    };
  },
  methods: {
    login (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios
              .post('http://localhost:8081/login', {
                account: this.loginForm.account,
                password: this.loginForm.password
              })
              .then(successResponse => {
                if (successResponse.data.code === 200) {
                  this.data=successResponse.data.data;
                  this.token=this.data.token;
                  this.user=this.data.user;
                  this.factoryid=this.data.factoryid;
                  this.factorystatusid=this.data.factorystatusid;

                  //存储token
                  this.$store.commit('SET_TOKENN', this.token);
                  //存储user，优雅一点的做法是token和user分开获取
                  this.$store.commit('SET_USER', this.user);
                  this.$store.commit('SET_FACTORYID', this.factoryid);
                  this.$store.commit('SET_FACTORYSTATUSID', this.factorystatusid);

                  this.$router.replace({path: '/index'});
                  this.$message.success("登陆成功，欢迎您 "+this.$store.state.user.name+" !");
                }else{
                  this.$message({
                    message: '账号或密码错误!',
                    type: 'error'
                  });
                }
              })
        }else{
          this.$message.error('请输入账号密码!');
          return false;
        }
      });


    },
    regester(){
      this.$router.push("/regester");
    }
  },
};
</script>

<style scoped>
#login-page {
  background: url("../../assets/img/bg.jpg") no-repeat center;
  height: 100%;
  width: 100%;
  background-size: cover;
  position: fixed;
}
body {
  margin: 0px;
}
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 90px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
</style>